//
//  FileTicketViewController.m
//  ELife
//
//  Created by  刘奇男 on 11-5-30.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "FilmTicketCityViewController.h"
#import "AlertView.h"
#import "GDataXMLNode.h"
#import "GTMBase64.h"



@implementation FilmTicketCityViewController

FilmTicketIndexViewController *indexController;

@synthesize cityNames, table;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
    
    [cityNames release];
    [table release];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    app = [ELifeAppDelegate App];
    [app showProgressDialog:@"请稍候"];
    
//    [table setDataSource:self];
//    [table setDelegate:self];
    
    /*10.7 查询所支持城市
     请求地址: /cinema.php
     请求参数: action  => get_city_list
     
     返回参数: 
     citys = 城市；
     city = 城市；
     
     返回格式事例: 
     <?xml version="1.0" encoding="UTF-8"?><msg><head><returncode>0</returncode><transcode>get_city_list</transcode></head><body><cities><city>上海</city><city>成都</city><city>杭州</city><city>天津</city><city>北京</city><city>呼和浩特</city><city>广州</city><city>深圳</city><city>海口</city><city>太原</city><city>武汉</city><city>重庆</city><city>福州</city><city>南京</city><city>运城</city><city>青岛</city><city>郑州</city><city>济南</city></cities></body></msg>*/
    
    NSString* host =  [[NSBundle mainBundle] localizedStringForKey:(@"BussinessHost") value:@"" table:nil];
    NSURL* url = [[NSURL alloc] initWithString:[host stringByAppendingString:@"cinema.php?"]];
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]initWithCapacity:1];
    [param setObject:@"get_city_list" forKey:@"action"];
    [param setObject:[[NSBundle mainBundle] localizedStringForKey:(@"ClientVersion") value:@"" table:nil] forKey:@"version"];
    
    NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:url, @"iRequestFinished:", nil] forKeys:[NSArray arrayWithObjects:@"url", @"requestMethod", nil]];
    
    [[app httpRequestPoint]openUrl:dic postParam:param target:self];
    
    [param release];
    
    [self.navigationItem setTitle:@"选择城市"];
}
#pragma mark -
#pragma mark 联网回调
- (void)iRequestFinished:(NSData *)response
{
    [app hideProgressDialog];
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[GTMBase64 decodeData:response] options:0 error:nil];
    GDataXMLElement* rootElement = [doc rootElement];    
    
    GDataXMLElement* bodyElement = [[rootElement elementsForName:@"body"]objectAtIndex:0];
    
    //某个具体节点的文本内容
    GDataXMLElement* citiesElement = [[bodyElement elementsForName:@"cities"]objectAtIndex:0];
    
    NSArray *cityArr = [citiesElement children];
    
    cityNames =  [[NSMutableArray alloc] initWithCapacity:[cityArr count]];
    for (int i = 0; i<[cityArr count]; i++) {
        GDataXMLElement *element = [cityArr objectAtIndex:i];
        
        NSArray *arr = [element children];
        for (int j = 0; j<[arr count]; j++) {
            GDataXMLNode *node = [arr objectAtIndex:j];
            [cityNames addObject:[node stringValue]];
        }
    }
    
   NSLog(@"--------cityNames:--------\n%@", cityNames);
    
    [doc release];

    [table reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [cityNames count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellIdentifier = @"CityTableCell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
	if (cell == nil)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellIdentifier] autorelease];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.opaque = NO;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:18];

    cell.textLabel.text = [cityNames objectAtIndex:indexPath.row];
	
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexController != nil){
        [indexController setCity:[cityNames objectAtIndex:indexPath.row]];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void) setIndexController:(FilmTicketIndexViewController *)controller
{
    indexController = controller;
}


/**
 CATransition *animation = [CATransition animation];
 //animation.delegate = self;
 animation.duration = 0.3f;
 animation.timingFunction = UIViewAnimationCurveEaseInOut;
 animation.fillMode = kCAFillModeForwards;
 animation.type = kCATransitionMoveIn;
 animation.subtype = kCATransitionFromTop;
 
 [myView.layer addAnimation:animation forKey:@"animation"];
 */
@end
